/**
 * based on http://mobiforge.com/designing/story/understanding-user-interface-android-part-4-even-more-views
 */
package de.bht.mi.kriz.multitouch;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import org.metalev.multitouch.visualizer2.MultiTouchVisualizerView;

public class MenuActivity extends Activity {    

    public boolean test = false;
    private View mView = null;
    static final int VIEWOPTIONS = 1;
    
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        mView = new MenuView(this);
        setContentView(mView);   
    }
    
    private class MenuView extends View {

    	private final String[] infoLines = {
    			"I M M S", 
    			"----------", 
    			"Multitouch Research Toolkit", 
    			"----------------",  
    			"Open the option menu", 
    			"to switch the playgrounds", 
    			"----------------", 
    			"----------",  
    			"(c) Oliver Kriz - 2011/2012" 
    	};
    	
    	private Paint mTouchTheScreenLabelPaint = new Paint();
    	
		public MenuView(Context context) {
			super(context);
			mTouchTheScreenLabelPaint.setColor(Color.GRAY);
			mTouchTheScreenLabelPaint.setTextSize(24);
			mTouchTheScreenLabelPaint.setTypeface(Typeface.DEFAULT_BOLD);
			mTouchTheScreenLabelPaint.setAntiAlias(true);
		}
		
		private void paintText(Canvas canvas, String msg, float vPos) {
			Rect bounds = new Rect();
			int msgLen = msg.length();
			mTouchTheScreenLabelPaint.getTextBounds(msg, 0, msgLen, bounds);
			canvas.drawText(msg, (canvas.getWidth() - bounds.width()) * .5f, vPos, mTouchTheScreenLabelPaint);
		}		
    	
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			
			float spacing = mTouchTheScreenLabelPaint.getFontSpacing();
			float totHeight = spacing * infoLines.length;
			for (int i = 0; i < infoLines.length; i++)
				paintText(canvas, infoLines[i], (canvas.getHeight() - totHeight) * .5f + i * spacing);
		}
    	
    	
    }
    
    private void CreateMenu(Menu menu)
    {
    	//Clearing menu at first
    	menu.clear();
    	SharedPreferences settings = getSharedPreferences("OptionMenu", MODE_PRIVATE);  

    	//Adding View Change Options
        menu.add(Menu.NONE, 0, Menu.NONE, "TouchPoints");
        menu.add(Menu.NONE, 1, Menu.NONE, "Elements");
        menu.add(Menu.NONE, 2, Menu.NONE, "Containers");
    	
        
        //Adding View specific options
        
        MenuItem curItem;
        String title = "";
    	
    	if(mView instanceof ElementView)
    	{
	        curItem = menu.add(VIEWOPTIONS, 0, Menu.NONE, "Fill Area");
	        {
	            title = "Fill Area: ";
            	title += settings.getBoolean("fillArea", false)==true ? "on" : "off";
            	curItem.setTitle(title);
	        }
	        
	        curItem = menu.add(VIEWOPTIONS, 1, Menu.NONE, "Add Balls");
	        {
	            title = "Add Balls: ";
            	title += settings.getBoolean("addBalls", false)==true ? "on" : "off";
            	curItem.setTitle(title);
	        }
	        
	        curItem = menu.add(VIEWOPTIONS, 2, Menu.NONE, "Flingmode");
	        {
	            title = "Flingmode: ";
            	title += settings.getBoolean("flingMode", false)==true ? "on" : "off";
            	curItem.setTitle(title);
	        }
	        
	        curItem = menu.add(VIEWOPTIONS, 3, Menu.NONE, "Bouncy Walls");
	        {
	            title = "Bouncy Walls: ";
            	title += settings.getBoolean("bouncyWalls", false)==true ? "on" : "off";
            	curItem.setTitle(title);
	        }	        
    	}
    }
 
    private boolean MenuChoice(MenuItem item)
    {        
    	SharedPreferences settings = getSharedPreferences("OptionMenu", MODE_PRIVATE);  
    	SharedPreferences.Editor prefEditor = settings.edit();  
    	String title = "";
    	   
    	if(item.getGroupId() == Menu.NONE)
    	{
    		Toast.makeText(this, "Launching "+item.getTitle()+" Playground View", Toast.LENGTH_LONG).show();
    		
	        switch (item.getItemId()) 
	        {
		        case 0: mView = new MultiTouchVisualizerView(this); break;
		        case 1: mView = new ElementView(this); break;
		        case 2: mView = new ContainerView(this); break;
		            
	        }
	        setContentView(mView);
	        return true;
    	}
        else if(item.getGroupId() == VIEWOPTIONS)
        {
	        if(mView instanceof ElementView) 
	        {
	        	switch (item.getItemId()) 
	        	{
		            case 0:
		            	prefEditor.putBoolean("fillArea", !settings.getBoolean("fillArea", false));  
		            	prefEditor.commit();  
		            	
		            	title = "Fill Area: ";
		            	title += settings.getBoolean("fillArea", false)==true ? "on" : "off";
		            	item.setTitle(title);
		            	break;
		            case 1:
		            	prefEditor.putBoolean("addBalls", !settings.getBoolean("addBalls", false));  
		            	prefEditor.commit();  
		            	
		            	title = "Add Balls: ";
		            	title += settings.getBoolean("addBalls", false)==true ? "on" : "off";
		            	item.setTitle(title);
		            	break;
		            case 2:
		            	prefEditor.putBoolean("flingMode", !settings.getBoolean("flingMode", false));  
		            	prefEditor.commit();  
		            	
		            	//if flingMode is disabled -> disable bouncyWalls
		            	if(settings.getBoolean("bouncyWalls", false) && !settings.getBoolean("flingMode", false))
		            	{
		            		prefEditor.putBoolean("bouncyWalls", !settings.getBoolean("bouncyWalls", false));  
			            	prefEditor.commit();
		            	}
		            	
			            title = "Flingmode: ";
		            	title += settings.getBoolean("flingMode", false)==true ? "on" : "off";
		            	item.setTitle(title);
		            	break;
		            case 3:
		            	prefEditor.putBoolean("bouncyWalls", !settings.getBoolean("bouncyWalls", false));  
		            	prefEditor.commit();  
		            	
		            	//if bouncyWalls is enabled -> enable flingMode
		            	if(settings.getBoolean("bouncyWalls", false) && !settings.getBoolean("flingMode", false))
		            	{
		            		prefEditor.putBoolean("flingMode", !settings.getBoolean("flingMode", false));  
			            	prefEditor.commit();
		            	}
		            	
		            	
			            title = "Bouncy Walls: ";
		            	title += settings.getBoolean("bouncyWalls", false)==true ? "on" : "off";
		            	
		            	item.setTitle(title);
		            	break;
	        	}
	        	mView.invalidate();
	        	return true;
	        }
        }
        return false;
    }    

    //---only created once---
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        CreateMenu(menu);
        return true;
    }
    
    // this function will be triggered, each time user opens options menu
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
    	
    	//FIXME: This will not be triggered on Android >=3.0, use  invalidateOptionsMenu() instead!
    	
    	super.onPrepareOptionsMenu(menu);
    	CreateMenu(menu);
    	return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {    
         return MenuChoice(item);    
    }
    

}